﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using DataAccessLayer;

namespace BusinessAccessLayer
{
    public class BAKhachHang
    {
        QuanLyBanHangDataContext dbs = new QuanLyBanHangDataContext();
       
        public DataTable getTable()
        {
            DataTable kh = new DataTable();
            kh.Columns.Add("MaKhachHang", typeof(string));
            kh.Columns.Add("TenCTY", typeof(string));
            kh.Columns.Add("Diachi", typeof(string));
            kh.Columns.Add("Thanhpho", typeof(string));
            kh.Columns.Add("Dienthoai", typeof(string));
            foreach (var item in dbs.KhachHangs.ToList())
            {
                DataRow row = kh.NewRow();
                row["MaKhachHang"] = item.MaKH;
                row["TenCTY"] = item.TenCty;
                row["Diachi"] = item.DiaChi;
                row["Thanhpho"] = item.ThanhPho;
                row["Dienthoai"] = item.DienThoai;
                kh.Rows.Add(row);
            }
            return kh;
        }
            
   
        public bool ThemKhachHang(string strMaKH, string strTenCTy, string strDiaChi, string strThanhPho, string strDienThoai)
        {
            bool f = false;
            try
            {
                KhachHang kh = new KhachHang()
                {
                    MaKH = strMaKH,
                    TenCty = strTenCTy,
                    DiaChi = strDiaChi,
                    ThanhPho = strThanhPho,
                    DienThoai = strDienThoai
                };
                dbs.KhachHangs.InsertOnSubmit(kh);
                dbs.SubmitChanges();
                f = true;
            }
            catch (Exception ms)
            {
                throw new Exception(ms.Message);
                f = false;
            }
            return f;
        }
        public bool XoaKhachHang(string strMaKH)
        {
            bool f = false;
            try
            {
                QuanLyBanHangDataContext dbs = new QuanLyBanHangDataContext();
                KhachHang khachhang = dbs.KhachHangs.Single(kh => kh.MaKH == strMaKH);
                dbs.KhachHangs.DeleteOnSubmit(khachhang);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool SuaKhachHang(string strMaKH, string strTenCty, string strDiaChi, string strThanhPho, string strDienThoai)
        {
            bool f = false;
            try
            {
                KhachHang khachhang =
                    dbs.KhachHangs.
                    Single(kh => kh.MaKH == strMaKH);
                khachhang.TenCty = strTenCty;
                khachhang.DiaChi = strDiaChi;
                khachhang.ThanhPho = strThanhPho;
                khachhang.DienThoai = strDienThoai;
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;

        }
    }
}